package com.chen.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.app.entity.User;
import com.chen.app.mapper.UserMapper;
import com.chen.app.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@CacheConfig(cacheNames = "user")
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User addUser(User user) {
        userMapper.insert(user);
        return user;
    }

    @Override
    @CachePut(key = "#p0")
    public User editUser(User user) {
        userMapper.updateById(user);
        return user;
    }

    @Override
    @CacheEvict(key = "#p0")
    public boolean delUser(long id) {
        int i = userMapper.deleteById(id);
        return i > 0 ? true : false;
    }

    @Override
    @Cacheable(key = "'selectById:' + #p0")
    public User selectById(long id) {
        return userMapper.selectById(id);
    }

    @Override
    @Cacheable(keyGenerator = "keyGenerator")
    public List<User> selectList() {
        return userMapper.selectList(null);
    }
}
